import logging
from django.shortcuts import render
from django.http import HttpResponse,JsonResponse
from verification import constants
from utils.captcha.captcha import captcha
from user.models import User
from utils.json_code import json_response
# Create your views here.

logger = logging.getLogger('django')
def image_code_view(request):
    text,image = captcha.generate_captcha()
    request.session['image_code'] = text
    request.session.set_expiry(constants.IMAGE_CODE_EXPIRY)
    logger.info('Image code :{}'.format(text))
    return HttpResponse(content=image,content_type='image/jpg')

# def check_user_views(request,username):
#     data = {
#         'error':'0',
#         'errmsg':'ok',
#         'data' : {
#         'username':username,
#         'count':User.objects.filter(username=username).count()
#     }
#     }
#     return JsonResponse(data)
def check_user_views(reqeuest,username):
    data = {
        'username':username,
        'count':User.objects.filter(username=username).count()
    }
    return json_response(data=data)
def check_mobile_views(reqeuest,mobile):
    data = {
        'mobile':mobile,
        'count':User.objects.filter(mobile=mobile).count()
    }
    return json_response(data=data)
